package com.youdianstar.app.ui.reward

import android.graphics.Typeface
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.viewpager2.widget.ViewPager2
import com.youdianstar.app.R
import com.youdianstar.app.BR
import com.youdianstar.app.common.Extra
import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.adapter.RedeemTabAdapter
import com.youdianstar.app.databinding.ActivityRedeemListBinding
import com.google.android.material.tabs.TabLayout
import org.koin.androidx.viewmodel.ext.android.viewModel


/**
 * File: RedeemListActivity
 * Author: 82149 Create: 2022/2/28 17:24
 * Changes (from 2022/2/28)
 * --------------------------------------------------
 * description: 兑换列表(优惠券中心 改)
 * ---------------------------------------------------
 *
 */
class RedeemListActivity :BaseActivity<ActivityRedeemListBinding, RewardViewModel>(){
    override fun layoutId(): Int = R.layout.activity_redeem_list

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): RewardViewModel {
        val model: RewardViewModel by viewModel()
        return model
    }

    //默认选择tab
    private var defaultIndex : Int = 0
    private var tvTabIndex1: TextView? = null
    private var tvTabIndex2: TextView? = null
    //积分余额
    private var pointBalance:Int = 0

    override fun initData() {
        super.initData()
        mBinding.headBar.initTitle(this,getString(R.string.coupon_couponCenter))

        defaultIndex = intent.getIntExtra(Extra.IT_TYPE,0)

        pointBalance = intent.getIntExtra(Extra.IT_DATA,0)

        val titleTagList = mutableListOf<String>()
        titleTagList.add("General")
        titleTagList.add("Merchant")

        val adapter = RedeemTabAdapter(this,titleTagList,pointBalance)
        mBinding.redeemPager.adapter = adapter

        mBinding.tabRedeem.let {
            val tabIndex1 = it.newTab()
            tabIndex1.setCustomView(R.layout.layout_tab_coupon)
            tabIndex1.tag = titleTagList[0]
            it.addTab(tabIndex1, true)
            tvTabIndex1 = tabIndex1.customView?.findViewById<TextView>(R.id.tab_text)!!
            tvTabIndex1?.text = getString(R.string.coupon_generalCoupon)

            val tabIndex2 = it.newTab()
            tabIndex2.setCustomView(R.layout.layout_tab_coupon)
            tabIndex2.tag = titleTagList[1]
            it.addTab(tabIndex2, false)
            tvTabIndex2 = tabIndex2.customView?.findViewById<TextView>(R.id.tab_text)!!
            tvTabIndex2?.text = getString(R.string.coupon_merchantCoupon)

            setTabUIStyle(defaultIndex)
            if (defaultIndex==0){
                it.selectTab(tabIndex1)
            } else {
                it.selectTab(tabIndex2)
            }
        }

        mBinding.tabRedeem.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                //修改样式
                val selectIndex = tab?.position ?:0
                mBinding.redeemPager.currentItem = selectIndex
                setTabUIStyle(selectIndex)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {}

            override fun onTabReselected(tab: TabLayout.Tab?) {}
        })

        mBinding.redeemPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                mBinding.tabRedeem.setScrollPosition(position, 0F, false)
                setTabUIStyle(position)
            }
        })

        mBinding.redeemPager.currentItem = defaultIndex

    }


    /**更新TabUI样式*/
    private fun setTabUIStyle(selectIndex : Int){
        tvTabIndex1?.let { tab1 ->
            tab1.setTextColor(ContextCompat.getColor(this, if(selectIndex==0) R.color.color_39404D else R.color.color_AAAAAC))
            tab1.setBackgroundResource(if(selectIndex==0) R.drawable.tab_item_coupon_select else R.drawable.bg_shape_trans)
            setTabTextFont(tab1,selectIndex==0)
        }

        tvTabIndex2?.let { tab2 ->
            tab2.setTextColor(ContextCompat.getColor(this, if(selectIndex==1) R.color.color_39404D else R.color.color_AAAAAC))
            tab2.setBackgroundResource(if(selectIndex==1) R.drawable.tab_item_coupon_select else R.drawable.bg_shape_trans)
            setTabTextFont(tab2,selectIndex==0)
        }
    }

    private fun setTabTextFont(tvView:TextView,isSelect: Boolean){
        if (isSelect){
            tvView.setTypeface(null,Typeface.BOLD)
        }else{
            val normalTypeface = Typeface.create("sans-serif-medium",Typeface.NORMAL)
            tvView.typeface = normalTypeface
        }
    }

    override fun initClickEvent() {

    }


}